/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-15 10:31:03
 * @LastEditors: YangJianFei
 * @LastEditTime: 2024-10-11 17:23:07
 * @FilePath: \yt_devop_app\lib\components\device\device_button_group.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:progress_loading_button/progress_loading_button.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/model/device_obj/properties_model.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/extensions.dart';

import '../log_utils.dart';

class DeviceButtonGroup extends StatefulWidget {
  final PropertiesModel model;
  // 按钮背景颜色
  final Color backgroudColor;
  // 按钮文字颜色
  final Color textColor;
  // 刷新图标颜色
  final Color refreshColor;
  // 按钮圆弧度
  final double borderRadius;

  /// 是否可编辑
  final bool disable;

  /// 提示语
  final String? tip;

  // 切换回调
  final Future Function(String newKey, dynamic newVal)? onChanged;

  const DeviceButtonGroup(
      {super.key,
      required this.model,
      this.disable = false,
      this.tip,
      this.backgroudColor = Colour.backgroundColor,
      this.textColor = Colour.otherTextColor,
      this.refreshColor = Colour.primaryColor,
      this.borderRadius = 5.0,
      this.onChanged});

  @override
  State<DeviceButtonGroup> createState() => _DeviceButtonGroupState();
}

class _DeviceButtonGroupState extends State<DeviceButtonGroup> {
  List<String> tagNameList = [];
  List tagValueList = [];

  @override
  void initState() {
    initTagsBySpecs();
    Log.d('disable==' + widget.disable.toString());
    super.initState();
  }

  didupdateWidget(covariant DeviceButtonGroup oldWidget) {
    if (oldWidget.model != widget.model) {
      initTagsBySpecs();
    }
    super.didUpdateWidget(oldWidget);
  }

  initTagsBySpecs() {
    tagNameList.clear();
    tagValueList.clear();
    var specs = (widget.model.dataType?.specs as Map?) ?? {};
    specs.forEach((key, value) {
      tagValueList.add(key);
      tagNameList.add(value.toString());
    });
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 48,
          alignment: Alignment.centerLeft,
          child: Text(
            widget.model.name ?? '',
            style: TextStyles.f14,
          ),
        ),
        buttonList(),
        tipWidget(),
        Gaps.vGap16
      ],
    );
  }

  Widget buttonList() {
    return Wrap(
      spacing: 12.w,
      runSpacing: 12,
      children: tagNameList
          .mapIndexed(
            (e, i) => Container(
              decoration: BoxDecoration(
                color: widget.backgroudColor,
                borderRadius: BorderRadius.circular(
                  widget.borderRadius,
                ),
              ),
              child: LoadingButton(
                  width: e.length > 5 ? null : 105.w,
                  height: 35,
                  borderRadius: widget.borderRadius,
                  type: LoadingButtonType.Text,
                  color: widget.refreshColor,
                  textcolor: buttonTextColor,
                  defaultWidget: Text(
                    e,
                    style: TextStyle(color: buttonTextColor),
                  ),
                  animate: true,
                  onPressed: () async {
                    if (widget.disable) {
                      return;
                    }
                    await widget.onChanged
                        ?.call(tagValueList[i], tagNameList[i]);
                  }),
            ),
          )
          .toList(),
    );
  }

  Widget tipWidget() {
    return Offstage(
      offstage: widget.tip == null,
      child: Padding(
        padding: const EdgeInsets.only(top: 6),
        child: Text(
          widget.tip ?? '',
          style: TextStyle(
              fontSize: 12.sp, color: Colour.warningTextColor, height: 1.5),
        ),
      ),
    );
  }

  Color get buttonBgColor =>
      widget.disable ? Colour.backgroundColor : widget.backgroudColor;

  Color get buttonTextColor =>
      widget.disable ? Colour.lightTextColor : widget.textColor;
}
